<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/3/1
 * Time: 下午5:02
 */

namespace app\common\Model;

require dirname(dirname(__DIR__)) . '/common/AliPay/alipay_notify.class.php';

/*require dirname(dirname(__DIR__)) . '/common/Ext/Alipay/aop/AopClient.php';
require dirname(dirname(__DIR__)) . '/common/Ext/Alipay/pagepay/service/AlipayTradeService.php';*/

class AliPayModel
{
    private $sign;

    private $param = array
    (
        'partner' => '',
        'seller_id' => '',
        'sign_type' => '',
        'out_trade_no' => '',
        'subject' => '',
        'body' => '',
        'total_fee' => '',        // 注意，此次单位为【元】
        'notify_url' => '',
        'service' => 'mobile.securitypay.pay',    // 移动支付
        'payment_type' => '1',
        '_input_charset' => 'utf-8',
        'it_b_pay' => '30m',
        'return_url' => 'm.alipay.com'
    );

    /**
     * @return string
     * $cost_id = '', $orderInfo, $total_fee
     */
    public function doAlPay($orderinfo)
    {
        //1 加载配置文件
        $conf = config('pay')['ALI_PAY'];

        //2 定义私钥 公钥
        $private_key_file_path = dirname(dirname(__DIR__)) . '/common/Cert/aliPayNewCert/rsa_private_key.pem';
        $this->param['partner'] = $conf['partner'];
        $this->param['seller_id'] = $conf['seller'];
        $this->param['sign_type'] = $conf['sign_type'];
        $this->param['out_trade_no'] = $orderinfo['order_num'];
        $this->param['subject'] = $orderinfo['body'];
        $this->param['body'] = $orderinfo['body'];
//        $this->param['total_fee'] = floatval($total_fee/100);
        $this->param['total_fee'] = floatval(1 / 100.00);
        $this->param['notify_url'] = $conf['notify_url'];

        $enc_sign_data;
        foreach ($this->param as $key => $value) {
            $enc_sign_data[$key] = '"' . $value . '"';
        }

        $para_filter = \paraFilter($enc_sign_data);
        $query = \createLinkstring($para_filter);
        return $query . "&sign=" . '"' . urlencode(rsaSign($query, $private_key_file_path)) . '"' . "&sign_type=" . '"' . "RSA" . '"';
    }

    public function refundAliPay($refound_order)
    {
        $conf = config('pay')['ALI_PAY'];
        $aop = new \AopClient ();

        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $conf['APPID'];
        $aop->rsaPrivateKey = $conf['ali_private_key'];
        $aop->alipayrsaPublicKey = $conf['ali_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';

        $request = new \AlipayTradeRefundRequest ();

        $param = [
            'out_trade_no' => 're201804092127474',
            'trade_no' => '2018040921001004200523907199',
            'refund_amount' => '0.01',
        ];
        $request->setBizContent(json_encode($param));
        $result = $aop->execute($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            echo "成功";
        } else {
            echo "失败";
        }
    }
}